package com.tangyuan.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tangyuan.common.enums.ConcernTypeEnum;
import com.tangyuan.common.result.Result;
import com.tangyuan.common.result.ResultCodeEnum;
import com.tangyuan.model.User;
import com.tangyuan.model.UserConcern;
import com.tangyuan.user.mapper.UserConcernMapper;
import com.tangyuan.user.service.IUserService;
import com.tangyuan.user.service.UserConcernService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class UserConcernServiceImpl extends ServiceImpl<UserConcernMapper, UserConcern> implements UserConcernService {

    @Autowired
    IUserService userService;

    //取消两个用户之间的相互关注
    @Override
    public void cancelConcernUser(Long userId, Long concernUserId) {
        baseMapper.cancelUserConcern(userId,concernUserId);
    }

    @Override
    public Result addConcern(User user, Long concernUserId,Integer type) {
        Long userId = user.getId();
        if (userId.equals(concernUserId)){
            return Result.build(null,666,"自己不可被关注");
        }
        User concernUser = userService.getUserById(concernUserId);
        if (concernUser==null){
            return Result.build(null,666,"该用户不可被关注");
        }
        //关注查询用户是否关注过选中用户
        UserConcern userConcern = baseMapper.checkIfConcern(userId, concernUserId);
        //没关注过 新增关注用户
        if (userConcern == null) {
            UserConcern concern = new UserConcern();
            concern.setUserId(userId);
            concern.setUserName(user.getName());
            concern.setConcernUserId( concernUserId);
            concern.setConcernUserName(concernUser.getName());
            concern.setConcernUserImage(concernUser.getHeadImage());
            concern.setType(ConcernTypeEnum.getEnum(type).type);
            concern.setCreateTime(new Date());
            baseMapper.insert(concern);
            return Result.ok();
        }
        //关注过且正在关注 取消关注状态
        if (userConcern.getIsDelete() == 0) {
            userConcern.setMarkName("");
            baseMapper.cancelUserConcern(userId, userConcern.getConcernUserId());
            return Result.ok();
        }
        //关注过没有取消了关注 改回关注状态
        if (userConcern.getIsDelete() == 1) {
            userConcern.setUserName(concernUser.getName());
            userConcern.setConcernUserId(concernUserId);
            userConcern.setConcernUserName(concernUser.getName());
            userConcern.setConcernUserImage(concernUser.getHeadImage());
            userConcern.setType(ConcernTypeEnum.getEnum(type).type);
            userConcern.setUpdateTime(new Date());
            baseMapper.updateUserConcern(userConcern);
            return Result.ok();
        }
        return Result.build(null, ResultCodeEnum.USER_NOT_EXIST);

    }

    @Override
    public List<UserConcern> selectUserConcernList(Long userId, Long concernUserId) {
        LambdaQueryWrapper<UserConcern> wrap = new LambdaQueryWrapper<>();
        wrap.eq(UserConcern::getUserId, userId);
        if (concernUserId != null){
            wrap.eq(UserConcern::getConcernUserId, concernUserId);
        }
        wrap.eq(UserConcern::getIsDelete, 0);
        return baseMapper.selectList(wrap);
    }

    @Override
    public Long getLiveConcernNum(Long concernUserId, Integer type,Long concernId) {
        if(concernUserId == null || concernUserId <= 0){
            return 0L;
        }
        return baseMapper.selectCount(new LambdaQueryWrapper<UserConcern>()
                .eq(UserConcern::getConcernUserId,concernUserId)
                .eq(type != null && type > 0,UserConcern::getType,type)
                .eq(concernId != null && concernId > 0,UserConcern::getConcernId,concernId));
    }

    @Override
    public Result<Boolean> isConcern(Long userId, Long concernUserId) {
        if(userId == null || userId == 0
        || concernUserId == null || concernUserId == 0){
            return Result.build(null,ResultCodeEnum.USER_NOT_EXIST);
        }
        return Result.ok(baseMapper.checkIfConcern(userId,concernUserId) != null);
    }


}
